<template>
    <div class="contain-box">
        <div class="box-header">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item>资金管理</el-breadcrumb-item>
                <el-breadcrumb-item to="/receipt" replace>付款单</el-breadcrumb-item>
                <el-breadcrumb-item>新增付款单</el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="box-body">
            <div class="body-top">付款单</div>
            <div class="body-set">
                <el-input v-model="addObj.name" placeholder="供应商名称" style="width: 200px;margin-right: 10px"
                          size="small"></el-input>
                <el-date-picker
                        v-model="addObj.moneyTime"
                        size="small"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="付款日期">
                </el-date-picker>
            </div>
            <div class="body-add">
                <el-button type="primary" style="background-color: #1abc9c" size="small" @click="showOrder">关联订单
                </el-button>
            </div>
            <div class="body-table">
                <el-table
                        :data="chooseData"
                        size="small"
                        border
                        style="width: 100%">
                    <el-table-column
                            type="index"
                            label="序号"
                            width="100px"
                            align="center"
                    >
                    </el-table-column>
                    <el-table-column
                            prop="orderNum"
                            label="关联采购订单"
                            align="center"
                    >
                    </el-table-column>
                    <el-table-column
                            label="应付款金额(元)"
                            align="center"
                    >
                        <template slot-scope="scope">
                            {{scope.row.discountMoney+scope.row.actualReceiptMoney}}
                        </template>
                    </el-table-column>
                    <el-table-column
                            prop="discountMoney"
                            label="优惠金额(元)"
                            align="center"
                    >
                    </el-table-column>
                    <el-table-column
                            prop="actualPay"
                            label="实付金额(元)"
                            align="center"
                    >
                    </el-table-column>
                    <el-table-column
                            prop="settlement.settlementName"
                            label="结算方式"
                            align="center"
                    >
                    </el-table-column>
                    <el-table-column
                            label="操作"
                            align="center"
                    >
                        <template slot-scope="scope">
                            <el-button @click="deleteObj(scope.$index)" size="small" type="text">删 除</el-button>
                        </template>
                    </el-table-column>

                </el-table>
            </div>
            <div style="margin-top: 20px">
                <p style="font-size: 16px">付款凭证 :</p>
                <el-upload
                        class="upload-demo"
                        :action="api+'/unitInfo/uploadFile'"
                        :on-preview="handlePreview"
                        :on-remove="handleRemove"
                        :before-remove="beforeRemove"
                        :on-success="onSuccess"
                        multiple
                        :headers="header"
                        :limit="1"
                        style="margin-top: 10px"
                        :on-exceed="handleExceed"
                        :file-list="fileList">
                    <el-button size="small" type="primary" style="background-color: #1abc9c">上 传</el-button>
                    <div slot="tip" class="el-upload__tip">（单个文件最大支持25M,支持格式Word、PDF、JPG、PNG、ZIP;)</div>
                </el-upload>
            </div>
            <div class="body-set">
                <el-input v-model="addObj.remarks" placeholder="请输入收款备注" type="textarea" style="width: 50%" :rows="5"
                          resize="none"
                          size="small"></el-input>
            </div>

        </div>
        <div class="box-submit">
            <div class="submit-left">
                <p>总计金额: <span>￥{{addObj.totalMoney}}</span></p>
                <p>实收金额: <span>￥{{addObj.actualMoney}}</span></p>
            </div>
            <div>
                <el-button @click="goBack" size="small">取 消</el-button>
                <el-button @click="submitAdd" type="primary" size="small" style="background-color: #1abc9c">提交审核
                </el-button>
            </div>
        </div>
        <el-dialog
                title="关联订单"
                :visible.sync="dialogTable"
                width="50%"
        >
            <div>
                <el-input clearable v-model="pageObj.customerName" placeholder="请输入订单编号/客户名称" style="width: 50%"
                          size="small">
                    <el-button @click="findAllOrder" slot="append" icon="el-icon-search"></el-button>
                </el-input>
                <el-table
                        :data="tableData"
                        size="mini"
                        tooltip-effect="dark"
                        style="width: 100%;margin-top: 20px"
                        @selection-change="handleSelectionChange">
                    <el-table-column
                            align="center"
                            type="selection"
                            width="55">
                    </el-table-column>
                    <el-table-column
                            prop="orderCreateTime"
                            label="订单日期"
                            align="center"
                    >
                    </el-table-column>

                    <el-table-column
                            prop="orderNum"
                            label="订单编号"
                            align="center"
                    >
                    </el-table-column>
                    <el-table-column
                            prop="supplierName"
                            label="供应商名称"
                            align="center"
                    >
                    </el-table-column>
                    <el-table-column
                            prop="actualPay"
                            label="实付金额"
                            align="center"
                    >
                    </el-table-column>
                    <el-table-column
                            prop="warehous.warehousName"
                            label="入库仓库"
                            align="center"
                    >
                    </el-table-column>
                </el-table>
                <div class="dialog-page">
                    <el-pagination
                            background
                            @current-change="handleSelectionChange2"
                            :current-page="pageObj.pageIndex"
                            :page-size="pageObj.pageSize"
                            layout="total, prev, pager, next, jumper"
                            :total="total">
                    </el-pagination>
                </div>
            </div>

            <span slot="footer" class="dialog-footer">
					<el-button @click="dialogTable = false" size="small">取 消</el-button>
					<el-button type="primary" @click="submitChoose" size="small"
                               style="background-color: #1abc9c">确 定</el-button>
				  </span>
        </el-dialog>
    </div>
</template>

<script>
    import common from "../utils/common";

    export default {
        data() {
            return {
                api: null,
                total: 0,
                pageObj: {
                    pageIndex: 1,
                    pageSize: 5,
                    queryStr: null,
                },
                dialogTable: false,
                tableData: [],
                chooseData: [],
                chooseList: [],
                addObj: {
                    moneyTime: null,
                    salesOrderId: [],
                    certificateUrl: null,
                    type: 2,
                    remarks: null,
                    name: null,
                    totalMoney: 0,
                    actualMoney: 0
                },
                fileList: [],
                header: {}
            }
        },
        created() {
            this.api = common.requestUrl
            this.header.token = sessionStorage.getItem('tokenValue')
        },
        methods: {
            deleteObj(index) {
                this.chooseData.splice(index, 1)
                this.addObj.salesOrderId = []
                this.addObj.totalMoney = 0
                this.addObj.actualMoney = 0
                if (this.chooseData.length !== 0) {
                    for (let i = 0; i < this.chooseData.length; i++) {
                        this.addObj.salesOrderId.push(this.chooseData[i].id)
                        this.addObj.totalMoney = this.addObj.totalMoney + this.chooseData[i].actualPay + this.chooseData[i].discountMoney
                        this.addObj.actualMoney = this.addObj.actualMoney + this.chooseData[i].actualPay
                    }
                }
            },
            submitAdd() {
                this.axios.post('/collectPayOrder/save', this.addObj).then(res => {
                    if (res.data.code === 200) {
                        this.goBack()
                        this.$message.success('提交成功')
                    }
                })
            },
            goBack() {
                this.$router.go(-1)
            },
            onSuccess(response, file, fileList) {
                if (response.code === 200) {
                    this.addObj.certificateUrl = response.data
                    this.$message.success(response.message)
                } else {
                    this.$message.warning(response.message)
                }
            },
            submitChoose() {
                this.chooseData = this.chooseList
                this.addObj.salesOrderId = []
                this.addObj.totalMoney = 0
                this.addObj.actualMoney = 0
                if (this.chooseData.length !== 0) {
                    for (let i = 0; i < this.chooseData.length; i++) {
                        this.addObj.salesOrderId.push(this.chooseData[i].id)
                        this.addObj.totalMoney = this.addObj.totalMoney + this.chooseData[i].actualPay + this.chooseData[i].discountMoney
                        this.addObj.actualMoney = this.addObj.actualMoney + this.chooseData[i].actualPay
                    }
                }
                this.dialogTable = false
            },
            showOrder() {
                this.pageObj.pageIndex = 1
                this.findAllOrder()
            },
            //查询全部销售订单
            findAllOrder() {
                this.axios.post('/purchaseOrder/findPage', this.pageObj).then(res => {
                    if (res.data.code === 200) {
                        this.tableData = res.data.data.data
                        this.total = res.data.data.total
                        this.dialogTable = true
                    }
                })
            },
            handleSelectionChange(val) {
                console.log(val)
                this.chooseList = val
            },
            handleSelectionChange2(currentPage) {
                this.pageObj.pageIndex = currentPage;
                this.findPage();
            },
            handleRemove(file, fileList) {
                console.log(file, fileList);
                this.addObj.certificateUrl = null
            },
            handlePreview(file) {
                console.log(file);
            },
            handleExceed(files, fileList) {
                this.$message.warning('当前只能上传一个文件');
            },
            beforeRemove(file, fileList) {
                return this.$confirm(`确定移除 ${file.name}？`);
            },

        }
    }
</script>

<style scoped lang="less">
    .contain-box {
        width: 100%;
        height: 100%;

        /deep/ .distpicker-address-wrapper {
            select {
                width: 32% !important;
                padding: 0px 10px !important;
                height: 35px !important;
                font-size: 12px !important;
                line-height: 35px !important;
                border-radius: 5px;
            }
        }

        .dialog-box {
            width: 100%;
        }

        .dialog-page {
            margin-top: 20px;
            width: 100%;
            display: flex;
            align-items: center;
            justify-content: flex-end;

            /deep/ .el-pagination.is-background .el-pager li:not(.disabled).active {
                background-color: #1abc9c;
            }
        }

        /deep/ .el-dialog__body {
            padding: 10px 20px;
        }

        .box-header {
            width: 100%;
            height: 50px;
            display: flex;
            align-items: center;
            margin-bottom: 10px;
        }

        .box-body {
            width: 100%;
            border-radius: 5px;
            padding: 20px;
            box-sizing: border-box;
            box-shadow: 0 0 5px 0 #e4e4e4;

            .body-top {
                width: 100%;
                height: 50px;
                text-align: center;
                line-height: 50px;
                border-bottom: 1px solid #e4e4e4;
                font-size: 20px;
                color: #666;
            }

            .body-set {
                margin-top: 20px;
                width: 100%;
                display: flex;
                align-items: center;
                justify-content: flex-start;
            }

            .body-add {
                margin-top: 20px;
                width: 100%;
            }

            .body-table {
                margin-top: 20px;
                width: 100%;
            }

            .body-message {
                margin-top: 20px;
                width: 100%;

                .message-box {
                    margin-top: 20px;
                    display: flex;
                    align-items: center;
                    cursor: pointer;

                    .message-img {
                        margin-left: 20px;
                        width: 20px;
                        height: 20px;
                    }

                    .message-son {
                        margin-right: 15px;
                        font-size: 14px;
                    }
                }
            }
        }

        .box-submit {
            margin-top: 20px;
            width: 100%;
            border-radius: 5px;
            padding: 20px;
            box-sizing: border-box;
            display: flex;
            align-items: center;
            justify-content: space-between;
            box-shadow: 0 0 5px 0 #e4e4e4;

            .submit-left {
                display: flex;
                font-size: 16px;

                p {
                    margin-right: 20px;

                    span {
                        color: red;
                        font-weight: bold;
                    }
                }
            }
        }
    }
</style>
